import java.io.File;

    public class test
    {

        public static long getDirectorySize(File directory) {
            long size = 0;
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        size += getDirectorySize(file); // 递归调用
                    } else {
                        size += file.length(); // 文件大小
                    }
                }
            }
            return size;
        }

        public static void main(String[] args) {
            java.util.Scanner scanner = new java.util.Scanner(System.in);
            System.out.print("请输入目录路径: ");
            String directoryPath = scanner.nextLine();
            File directory = new File(directoryPath);

            if (directory.exists() && directory.isDirectory()) {
                long size = getDirectorySize(directory);
                System.out.println("目录 '" + directoryPath + "' 占用的空间大小为: " + size + " 字节");
            } else {
                System.out.println("指定的路径不是一个有效的目录！");
            }

            scanner.close();
        }
    }